package site.isscloud.jt809;


// 报警信息类

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class AlarmInfo {
    private String vehicleNo;      // 车牌号
    private byte vehicleColor;    // 车牌颜色
    private int alarmType;        // 报警类型 0x01:超速 0x02:疲劳驾驶
    private long alarmTime;       // 报警时间
    private double longitude;     // 经度
    private double latitude;      // 纬度
    private short speed;          // 速度
    private short direction;      // 方向
    private int alarmStatus;      // 报警状态 0:未处理 1:已处理
    private String alarmDesc;     // 报警描述
    
    // Constructor
    public AlarmInfo(String vehicleNo, byte vehicleColor, int alarmType, 
                    long alarmTime, double longitude, double latitude, 
                    short speed, short direction, int alarmStatus, String alarmDesc) {
        this.vehicleNo = vehicleNo;
        this.vehicleColor = vehicleColor;
        this.alarmType = alarmType;
        this.alarmTime = alarmTime;
        this.longitude = longitude;
        this.latitude = latitude;
        this.speed = speed;
        this.direction = direction;
        this.alarmStatus = alarmStatus;
        this.alarmDesc = alarmDesc;
    }
    
    // Getters
    public String getVehicleNo() {
        return vehicleNo;
    }
    
    public byte getVehicleColor() {
        return vehicleColor;
    }
    
    public int getAlarmType() {
        return alarmType;
    }
    
    public long getAlarmTime() {
        return alarmTime;
    }
    
    public double getLongitude() {
        return longitude;
    }
    
    public double getLatitude() {
        return latitude;
    }
    
    public short getSpeed() {
        return speed;
    }
    
    public short getDirection() {
        return direction;
    }
    
    public int getAlarmStatus() {
        return alarmStatus;
    }
    
    public String getAlarmDesc() {
        return alarmDesc;
    }
    
    // Setters
    public void setVehicleNo(String vehicleNo) {
        this.vehicleNo = vehicleNo;
    }
    
    public void setVehicleColor(byte vehicleColor) {
        this.vehicleColor = vehicleColor;
    }
    
    public void setAlarmType(int alarmType) {
        this.alarmType = alarmType;
    }
    
    public void setAlarmTime(long alarmTime) {
        this.alarmTime = alarmTime;
    }
    
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
    
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }
    
    public void setSpeed(short speed) {
        this.speed = speed;
    }
    
    public void setDirection(short direction) {
        this.direction = direction;
    }
    
    public void setAlarmStatus(int alarmStatus) {
        this.alarmStatus = alarmStatus;
    }
    
    public void setAlarmDesc(String alarmDesc) {
        this.alarmDesc = alarmDesc;
    }
    
    public byte[] toByteArray() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        
        dos.write(JT809Utils.stringToBytes(vehicleNo, 21));
        dos.writeByte(vehicleColor);
        dos.writeInt(alarmType);
        dos.writeLong(alarmTime);
        dos.writeInt((int)(longitude * 1000000));
        dos.writeInt((int)(latitude * 1000000));
        dos.writeShort(speed);
        dos.writeShort(direction);
        dos.writeInt(alarmStatus);
        dos.write(JT809Utils.stringToBytes(alarmDesc, 256));
        
        return baos.toByteArray();
    }
    
    public static AlarmInfo fromByteArray(byte[] data) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);
        
        String vehicleNo = JT809Utils.bytesToString(dis.readNBytes(21));
        byte vehicleColor = dis.readByte();
        int alarmType = dis.readInt();
        long alarmTime = dis.readLong();
        double longitude = dis.readInt() / 1000000.0;
        double latitude = dis.readInt() / 1000000.0;
        short speed = dis.readShort();
        short direction = dis.readShort();
        int alarmStatus = dis.readInt();
        String alarmDesc = JT809Utils.bytesToString(dis.readNBytes(256));
        
        return new AlarmInfo(vehicleNo, vehicleColor, alarmType, alarmTime, 
                           longitude, latitude, speed, direction, alarmStatus, alarmDesc);
    }

    @Override
    public String toString() {
        return String.format("AlarmInfo[车牌:%s, 类型:%d, 时间:%tF %tT, 位置:%.6f,%.6f, 状态:%d]",
                vehicleNo, alarmType, alarmTime, alarmTime, longitude, latitude, alarmStatus);
    }
}

